package com.tools.service.impl;

import com.tools.commons.utils.BeanUtils;
import com.tools.model.Request.listUserRequest;
import com.tools.model.entity.InfoEntity;
import com.tools.model.entity.UserEntity;
import com.tools.repository.InfoRepository;
import com.tools.repository.UserRepository;
import com.tools.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {
    private final UserRepository userRepository;
    private  final InfoRepository infoRepository;

    @Override
    public void save(UserEntity entity) {
        userRepository.save(entity);
    }

    @Override
    public UserEntity loginInPhone(String phone, boolean doctor) {
        List<UserEntity> byPhone = userRepository.findByPhone(phone);
        if (!CollectionUtils.isEmpty(byPhone)) {
            for (UserEntity userEntity : byPhone) {
                if (userEntity.isDoctor() == doctor) {
                    return userEntity;
                }
            }
        }
        return null;
    }

    @Override
    public UserEntity findByUserId(String userId) {
        return userRepository.findByUserId(userId);
    }

    @Override

    public UserEntity findByPhone(String phone) {

        List<UserEntity> userEntities = userRepository.findByPhone(phone);
        for (int i = 0; i < userEntities.size(); i++) {
            if (userEntities.get(i).isDoctor() == false) {
                return userEntities.get(i);
            }
        }
        return null;
    }
        @Override
    public List<listUserRequest> listAll(boolean isDoctor) {

          List<listUserRequest>  list  = new ArrayList<>();
          List<UserEntity> userEntityList=  userRepository.findAll((Specification<UserEntity>) (root, criteriaQuery, criteriaBuilder) -> criteriaBuilder.equal(root.get("isDoctor"), false));
            for (int i = 0; i <userEntityList.size() ; i++) {
                listUserRequest listUserRequest=new listUserRequest();
                listUserRequest.setCreateTime(userEntityList.get(i).getCreateTime());
                listUserRequest.setDoctor(isDoctor);
                listUserRequest.setPhone(userEntityList.get(i).getPhone());
                listUserRequest.setStatus(userEntityList.get(i).getStatus());
                listUserRequest.setUserId(userEntityList.get(i).getUserId());
                InfoEntity infoEntity =infoRepository.findAllByUserId(listUserRequest.getUserId());
               if(infoEntity!=null){
                   listUserRequest.setName(infoEntity.getName());

               }
                list.add(listUserRequest);
            }

        return list;

    }
}
